/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.language2;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import ac.jp.u_tokyo.SyncLib.language.EvaluationFailedException;
import ac.jp.u_tokyo.SyncLib.language.GraphCombinatorBody;
import ac.jp.u_tokyo.SyncLib.language.SwitchBody;
import ac.jp.u_tokyo.SyncLib.language.SyncBody;

public class Disjunction extends SyncConstruct
{
	public Disjunction(int line)
	{
		super(line);
	}

	private LinkedList<SyncConstruct> _inners = new LinkedList<SyncConstruct>();
	
	public void addInner(SyncConstruct body) {
		_inners.add(body);
	}
	
	public int getInnerCount() {
		return _inners.size();
	}
	
	public SyncConstruct getFirst() {
		return _inners.getFirst();
	}

	@Override
	public Set<Var> getVarUsed(Collection<String> genericParas)
			throws EvaluationFailedException
	{
		Set<Var> result = new HashSet<Var>();
		for(SyncConstruct body : _inners) {
			result.addAll(body.getVarUsed(genericParas));
		}
		return result;
	}

	@Override
	public void addBody(GraphCombinatorBody body,
			Collection<String> genericParas) throws EvaluationFailedException
	{
		List<Var> parameters = new LinkedList<Var>(getVarUsed(genericParas));
		SyncBody sync = getBody(parameters, genericParas);
		body.addConnection(sync, Helper.higherVar2LowerVar(parameters));
		
	}

	@Override
	public SyncBody getBody(List<Var> imposedVars,
			Collection<String> genericParas) throws EvaluationFailedException
	{
		SwitchBody body = new SwitchBody(getLine());
		for(SyncConstruct s : _inners) {
			body.addInnerSync(s.getBody(imposedVars, genericParas));
		}
		return body;
	}

}
